const fs = require('fs')
const path = require("path");

function saveAudio(file) {
    return new Promise((res, rej) => {
        try {
            //创建文件的存储路径，并对文件进行重命名
            // let filePath = path.join(__dirname, '../public/audio/') + `${file.originalFilename}`;
            const audioSuffix = file.originalFilename.split('.')[1]
            let filePath = path.join(__dirname, '../public/audio/') + "背景音乐." + audioSuffix;
            //读取文件，返回文件流
            const rs = fs.createReadStream(file.filepath);
            const rs2 = fs.createWriteStream(filePath)
            rs.pipe(rs2);
            rs2.on('finish', () => {
                res('/audio/' + "背景音乐." + audioSuffix)
            })
        } catch (e) {
            rej(e)
        }
    })
}


module.exports = saveAudio
